package com.zxh.blog.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zxh.blog.R
import com.zxh.blog.bean.Article
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob

import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class ArticleRecyclerAdapter(private val context: Context) : RecyclerView.Adapter<ArticleRecyclerAdapter.ViewHolder>() {

    // 文章列表
    private var mArticleList = ArrayList<Article>()

    // 协程管理
    private val mScope = CoroutineScope(Dispatchers.IO + SupervisorJob())

    private var mListener: ClickListener? = null

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        // 文章标题
        val articleTitle: TextView = view.findViewById(R.id.article_title)

        // 文章摘要
        val articleSummary: TextView = view.findViewById(R.id.article_summary)

        // 文章发布时间
        val articlePublishTime: TextView = view.findViewById(R.id.article_publish_time)

        // 文章分类
        val articleCategory: TextView = view.findViewById(R.id.article_category)

        // 文章缩略图
        val articleThumbnail: ImageView = view.findViewById(R.id.article_thumbnail)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_article, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mArticleList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        // 使用协程方式加载图片
        mScope.launch {
            try {
                val article = mArticleList[position]
                val glide = withContext(Dispatchers.IO) {
                    Glide.with(context)
                    // 获取 Glide 实例，在 IO 线程执行
                }

                // 加载缩略图
                val bitmap = withContext(Dispatchers.IO) {
                    glide.asBitmap()
                        .load(article.thumbnail)
                        .placeholder(R.drawable.icon_like) // 可选，占位符图片
                        .error(R.drawable.icon_add) // 可选，加载错误时的图片
                        .submit() // 提交加载请求
                        .get() // 获取返回的 Bitmap
                }

                // 切换回主线程更新UI
                launch(Dispatchers.Main) {
                    // 其他数据绑定操作
                    holder.articleThumbnail.setImageBitmap(bitmap)
                    holder.articleTitle.text = article.title
                    holder.articleSummary.text = article.summary
                    holder.articlePublishTime.text = article.createTime
                    holder.articleCategory.text = article.categoryName
                }
            } catch (e: Exception) {
                // 处理异常
                e.printStackTrace()
            }
        }

        holder.itemView.setOnClickListener {
            mListener?.onClick(position, mArticleList)
        }

    }


    interface ClickListener {
        fun onClick(position: Int, articleList: ArrayList<Article>)
    }

    fun setClickListener(listener: ClickListener) {
        mListener = listener
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setData(list: ArrayList<Article>) {
        mArticleList = list
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun update(list: ArrayList<Article>) {
        for (article in list) {
            mArticleList.add(article)
        }
        notifyDataSetChanged()
    }

}